package com.thinkfit.jy.view;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;


import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;

import java.lang.reflect.Field;

public class RightSpeedSetView implements View.OnClickListener {

    private static RightSpeedSetView rightSpeedSetView;
    WindowManager windowManager;
    Context mContext;
    View inflate;
    boolean isFist = true;
    boolean isSpeed = false;
    ConstraintLayout clBg;
    TextView tvItem1,tvItem2,tvItem3,tvItem4;

    float speedf;
    int speedInt;

    public static RightSpeedSetView getInstance(){
        if(rightSpeedSetView == null){
            rightSpeedSetView = new RightSpeedSetView();
        }
        return rightSpeedSetView;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_left_speed_set, null);
            clBg = inflate.findViewById(R.id.cl_bg);
            tvItem1 = inflate.findViewById(R.id.tv_item1);
            tvItem2 = inflate.findViewById(R.id.tv_item2);
            tvItem3 = inflate.findViewById(R.id.tv_item3);
            tvItem4 = inflate.findViewById(R.id.tv_item4);

            tvItem1.setOnClickListener(this);
            tvItem2.setOnClickListener(this);
            tvItem3.setOnClickListener(this);
            tvItem4.setOnClickListener(this);
            clBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goneLayout();
                }
            });

        }







        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.CENTER;

        System.out.println("ddddddddddddddddddddddd  isFist : "+isFist);

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            System.out.println("ddddddddddddddddddddddd  inflate : "+(inflate == null));
            inflate.setVisibility(View.VISIBLE);
        }
    }

    public EditText getNb(NumberPicker numberPicker){
        Field npe = null;
        try {
            npe = numberPicker.getClass().getDeclaredField( "mInputText");
            npe.setAccessible(true);
            try {
                ((EditText)npe.get(numberPicker)).setTextSize(30);
                ((EditText)npe.get(numberPicker)).setTextColor(mContext.getResources().getColor(R.color.white));
                Typeface typeface = Typeface.create("sans-serif-bold", Typeface.NORMAL);
                ((EditText)npe.get(numberPicker)).setTypeface(typeface);
                return ((EditText)npe.get(numberPicker));
            }catch (IllegalAccessException e){
                e.printStackTrace();
            }
        }catch (NoSuchFieldException e){
            e.printStackTrace();
        }
        return null;

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_item1:
                speedInt = 30;
                setSpeed(speedInt);
                break;
            case R.id.tv_item2:
                speedInt = 60;
                setSpeed(speedInt);
                break;
            case R.id.tv_item3:
                speedInt = 90;
                setSpeed(speedInt);
                break;
            case R.id.tv_item4:
                speedInt = 120;
                setSpeed(speedInt);
                break;

        }
        goneLayout();
    }



    /**
     * 设置速度
     * @param isAdd 判断是否是增加
     */
//    private void resetSpeed(boolean isAdd) {
//        isSpeed = true;
//        String textById = tvSpeed.getText().toString().trim();
//        if(textById.length()>=5){
//            return;
//        }
//        BigDecimal speed = new BigDecimal(textById);
//        if (isAdd) {
//            speed = speed.add(new BigDecimal("0.1"));
//            speedf = speed.floatValue();
//            if(speedf >= EquipmentInfoUtils.getInstance().getResistance()){
//                speedf = EquipmentInfoUtils.getInstance().getResistance()/10f;
//            }
//        } else {
//            speed = speed.subtract(new BigDecimal("0.1"));
//            speedf = speed.floatValue();
//            if(speedf <= 1f){
//                speedf = 1;
//            }
//        }
//        if (speed.compareTo(BigDecimal.ZERO) < 0) {
//            return;
//        }
//        speedInt = (int) (speedf*10f);
//        tvSpeed.setText(Utils.getNewFloat(speedf));
//        setSpeed(speedInt);
//        isLongClick = false;
//
//    }


    private void setSpeed(int speed){
        int slop = MotionDataUtils.getInstance().getCurrentInfo().slop;
        if(DeviceTypeUtils.connectDeviceType() == 0){
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  speed : "+speed);
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  speed   speedInt: "+speed);
            if(DeviceTypeUtils.getInstance().isTreadmill()){
                BluetoothOrder.getInstance().setTreadmillSpeedAndSlope(speed,slop);
            }else {
                BluetoothOrder.getInstance().setNowelAndSlope(speed,slop);
            }
        }else {
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa  speed11111 : "+speed);
            SerialPortManager.getInstance().setSpeedSlop(speed, slop);
        }
        isSpeed = false;
    }



    public void goneLayout(){
        if(inflate != null){
            inflate.setVisibility(View.GONE);
        }
    }

    /**
     * dp2px
     */
    protected int dp2px(float dpValue) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }


}
